<template>
  <div class="app-container">
    <basic-container>
      <avue-crud ref="crud" v-model="form" :page="jkdnPage" :data="tableData" @on-load="getList" :option="tableOption" :table-loading="jkdnLoading" @search-change="searchChange" @refresh-change="refreshChange" @row-save="rowSave" @row-del="rowDel">
        <template slot-scope="scope" slot="menuLeft">
          <el-button icon="el-icon-plus" size="small" type="primary" @click="handleAdd">新增</el-button>
        </template>
        <template slot-scope="scope" slot="menuForm">
          <el-button type="primary" size="medium" @click="getIdCard()">识别身份证</el-button>
        </template>
        <template slot-scope="scope" slot="menu">
          <el-button @click="rowEdit(scope.row)" icon="el-icon-edit" size="small" type="primary">编辑</el-button>
          <el-button @click="rowDel(scope.row)" icon="el-icon-delete" size="small" type="danger">删除</el-button>
        </template>
      </avue-crud>
    </basic-container>
  </div>
</template>

<script>
import { setStore, getStore } from '@/util/store'
import { getIdCard, getObjByIdCard } from '@/api/cust/base'
import tableOption from '@/const/cust/menu/oldList'
import { fetchList, getByTiaowen } from '@/api/cust/health/zyyjk'
import { delObj, addObj } from '@/api/cust/health/jkdn'
export default {
  name: 'crud',
  data() {
    return {
      grjbxxid: null,
      url: [],
      searchFrom: {},
      form: {},
      jkdnLoading: false,
      jkdnPage: {
        total: 0,
        currentPage: 1,
        pageSize: 10
      },
      org: {},
      tableOption: tableOption,
      tableData: []
    }
  },
  created() {
    this.org = getStore({ name: 'org' })
    this.getList()
  },
  watch: {
    'form.rzfs'() {
      const column1 = this.tableOption.column[5]
      const column2 = this.tableOption.column[2]
      const column3 = this.tableOption.column[3]
      if (this.form.rzfs === '1') {
        column1.addDisplay = true
        column2.addDisplay = false
        column3.addDisplay = false
      } else if (this.form.rzfs === '2') {
        column1.addDisplay = false
        column2.addDisplay = true
        column3.addDisplay = true
      }
    }
  },
  mounted: function() {},
  computed: {},
  methods: {
    //身份证识别仪
    getIdCard() {
      getIdCard().then(res => {
        var result = res.data
        if (typeof res.data == 'object') {
          this.$message.error('身份证识别失败，请重试')
          return
        }

        var data = res.data == null ? '' : res.data.replace(/\\/g, '/')
        try {
          result = JSON.parse(data)
        } catch (e) {
          result = eval('(' + data + ')')
        }
        if (result.ret === 0) {
          this.form.name = result.Certificate.Name
          this.form.sfzh = result.Certificate.IDNumber
        } else {
          this.$message.error('身份证识别失败，请重试')
        }
      })
    },
    //搜索
    searchChange(params) {
      this.searchform = params
      this.getList(this.jkdnPage, params)
    },
    //刷新
    refreshChange() {
      this.getList(this.jkdnPage, this.searchform)
    },
    //返回基本信息
    handleBack() {
      this.$router.push(`/cust/records`)
    },
    //新增
    handleAdd: function() {
      this.$refs.crud.rowAdd()
      setTimeout(() => {
        var date = new Date()
        this.form.lrsj =
          date.getFullYear() +
          '-' +
          (date.getMonth() + 1) +
          '-' +
          date.getDate()
      }, 300)
    },
    //删除
    rowDel(row) {
      this.$confirm('是否确认删除', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          delObj(row.id).then(() => {
            this.$message({
              showClose: true,
              message: '删除成功',
              type: 'success'
            })
            this.getList(this.jkdnPage, this.searchform)
          })
        })
        .catch(function() {})
    },
    rowEdit(row) {
      setStore({ name: 'jkdn', content: row })
      this.$router.push({
        path: `/cust/collect`,
        query: { id: row.jktjid, grjbxxid: row.grjbxxid }
      })
    },
    rowSave(row, done) {
      this.jkdnLoading = true
      if (row.rzfs === '1') {
        getByTiaowen({ tmcode: row.tmcode }).then(res => {
          if (this.validatenull(res.data.data)) {
            this.$message({
              showClose: true,
              message: '条纹码内容错误，请核对后重试',
              type: 'error'
            })
            return
          } else {
            var content = res.data.data
            content.lrr = row.lrr
            content.lrsj = row.lrsj
            content.jgbh = this.org.jgbh

            this.$router.push({
              path: `/cust/collect`,
              query: {
                grjbxxid: res.data.data.grjbxxid,
                id: res.data.data.id
              }
            })
          }
        })
      } else {
        getObjByIdCard({
          sfzh: this.form.sfzh,
          start: 0,
          length: 10
        }).then(res => {
          if (this.validatenull(res.data.data)) {
            this.$message({
              showClose: true,
              message: '身份证号错误，请核对后重试',
              type: 'error'
            })
            return
          } else {
            var content = res.data.data
            content.lrr = row.lrr
            content.lrsj = row.lrsj
            content.jgbh = this.org.jgbh
            console.log(content)

            setStore({
              name: 'jkdn',
              content: content
            })
            this.$router.push({
              path: `/cust/collect`,
              query: {
                grjbxxid: res.data.data.grjbxxid,
                id: res.data.data.id
              }
            })
          }
        })
      }
      this.jkdnLoading = false
      done()
    },
    //获取列表
    getList(page, params = {}) {
      this.jkdnLoading = true
      fetchList(
        Object.assign(
          // { grjbxxid: this.grjbxxid },
          params,
          {
            start: (page.currentPage - 1) * page.pageSize,
            length: page.pageSize
          }
        )
      ).then(res => {
        this.jkdnLoading = false
        this.jkdnPage.total = res.data.recordsTotal
        this.tableData = res.data.data
      })
      this.jkdnLoading = false
    },
    breaks() {
      this.$router.$avueRouter.closeTag()
    }
  }
}
</script>